<?php

class M_tagcloud extends Module {
  public function main() {
    $sql = "
        SELECT `tags_names`.`name`, `tags_names`.`permalink`, count_postsbytag(`tags`.`id`) AS `count`
        FROM `tags`
        INNER JOIN `tags_names` ON `tags`.`name`=`tags_names`.`id`
        HAVING `count`>0
        ORDER BY `count` DESC";
//    $sql = "SELECT `name`, `permalink`, COUNT(*) AS `count`
//        FROM `tags_names`
//        WHERE `post`>0
//        GROUP BY `tag`
//          HAVING `count`>0
//        ORDER BY `count` DESC";
    if ( !self::param('all') ) $sql .= "
        LIMIT 30";
    $res = db::q($sql)->arr();

    $max = $res[0]['count']+0;
    $min = $res[count($res)-1]['count']+0;
    if ( $max === $min ) $max = $min+1;

    sort($res);
    $tmp = '';

    if ( $res ) {
      $tmp = new Template( 'tagcloud' );
      $tmp->assign( 'min', $min );
      $tmp->assign( 'max', $max );
      $tmp->assign( 'all', self::param('all') );
      
      foreach  ( $res as $r ) {
        $tmp->add( 'tag', $r );
      }
    }
    return $tmp;
  }
}

?>